package com.njwb.day11.dvd;

import java.util.*;

public class DvdDaoImpl implements DvdDao {
//    ArrayList<Dvds> dvdList = new ArrayList<>();
    Map<String,Dvds> dvdsMap=new HashMap<>();
    public void moren() {
        dvdsMap.put("名侦探柯南", new Dvds("名侦探柯南", null, 1, 0));
        dvdsMap.put("数码宝贝", new Dvds("数码宝贝", null, 1, 4));
        dvdsMap.put("康熙王朝", new Dvds("康熙王朝", "2025-07-31", 2, 7));
        dvdsMap.put("甄嬛传", new Dvds("甄嬛传", "2025-06-30", 2, 3));
        dvdsMap.put("变形金刚", new Dvds("变形金刚", "2024-01-31", 2, 5));
        dvdsMap.put("还珠格格", new Dvds("还珠格格", "2025-07-27", 2, 6));
    }

    @Override
    public Boolean add(String name) {
      if(dvdsMap.containsKey(name)){
          return false;
      }

        dvdsMap.put(name, new Dvds(name, null, 1, 0));
        return true;

    }

    @Override
    public String delete(String name) {
      /*  Iterator<Dvds> iterator = dvdList.iterator();
        while (iterator.hasNext()) {
            Dvds dvd = iterator.next();
            if (dvd.getDvdName().equals(name)) {
                if (dvd.getStatus()==1){
                    iterator.remove();
                    return name+"删除成功";}
                else{return name+"已借出无法删除";}
            }
        }*/
        Dvds dvd = dvdsMap.get(name);
        if (dvd == null) {
            return name + "不存在";
        }
        if (dvd.getStatus() == 2) {
            return name + "已借出无法删除";
        }
        dvdsMap.remove(name);
        return name + "删除成功";
    }

    @Override
    public Dvds select(String name) {
        Dvds dvds=dvdsMap.get(name);
        if (dvds!=null){
            return dvds;
        }
return null;
    }

    @Override
    public String loan(String name) {
        Dvds dvds=dvdsMap.get(name);
        if (dvds!=null){
            if (dvds.getStatus()==1){
                dvds.setStatus(2);
                dvds.setCount(dvds.getCount()+1);
                dvds.setLendDate(java.time.LocalDate.now().toString());
                return name+"借出成功！！";
            } else {
                return name+" 已被借出！！";
            }
            }

        return name+"未找到";
    }

    @Override
    public String returns(String name) {
        Dvds dvds=dvdsMap.get(name);
        if (dvds!=null){
            if (dvds.getStatus()==2){
                dvds.setStatus(1);
                dvds.setLendDate(null);
                return name+"归还成功！！";
            }
            else{
                return name+"无归还";
            }
        }
        return name+"未找到";
    }
    @Override
    public Map<String, Dvds> showdesc() {
        // 先转为 List 以便排序
        List<Map.Entry<String, Dvds>> list = new ArrayList<>(dvdsMap.entrySet());

        // 使用 compareTo 自动排序（因为你实现了 Comparable<Dvds>）
        list.sort(Map.Entry.comparingByValue()); // 会调用 Dvds 的 compareTo 方法

        // 放入 LinkedHashMap 保持顺序
        Map<String, Dvds> sortedMap = new LinkedHashMap<>();
        for (Map.Entry<String, Dvds> entry : list) {
            sortedMap.put(entry.getKey(), entry.getValue());
        }

        return sortedMap;
    }



}
